package com.ruoyi.system.service.impl;

import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.constant.SysConfigConstants;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.utils.CacheUtils;
import com.ruoyi.system.domain.JiajiaTransferAccounts;
import com.ruoyi.system.domain.JiajiaUser;
import com.ruoyi.system.domain.JiajiaWorker;
import com.ruoyi.system.domain.JiajiaWorkerRenzheng;
import com.ruoyi.system.mapper.JiajiaWorkerRenzhengMapper;
import com.ruoyi.system.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 身份认证Service业务层处理
 * 
 * @author ruoyi
 * @date 2021-10-14
 */
@Service
public class JiajiaWorkerRenzhengServiceImpl implements IJiajiaWorkerRenzhengService
{
    @Autowired
    private JiajiaWorkerRenzhengMapper jiajiaWorkerRenzhengMapper;

    @Autowired
    private IJiajiaWorkerService iJiajiaWorkerService;

    @Autowired
    private IJiajiaUserService  userService;


    @Autowired
    private IJiajiaTransferAccountsService jiajiaTransferAccountsService;
    /**
     * 查询身份认证
     * 
     * @param id 身份认证主键
     * @return 身份认证
     */
    @Override
    public JiajiaWorkerRenzheng selectJiajiaWorkerRenzhengById(Long id)
    {
        return jiajiaWorkerRenzhengMapper.selectJiajiaWorkerRenzhengById(id);
    }

    @Override
    public JiajiaWorkerRenzheng selectJiajiaWorkerRenzhengByWid(Long wid) {
        return jiajiaWorkerRenzhengMapper.selectJiajiaWorkerRenzhengByWid(wid);
    }

    /**
     * 查询身份认证列表
     * 
     * @param jiajiaWorkerRenzheng 身份认证
     * @return 身份认证
     */
    @Override
    @DataScope(deptAlias = "d")
    public List<JiajiaWorkerRenzheng> selectJiajiaWorkerRenzhengList(JiajiaWorkerRenzheng jiajiaWorkerRenzheng)
    {
        return jiajiaWorkerRenzhengMapper.selectJiajiaWorkerRenzhengList(jiajiaWorkerRenzheng);
    }

    /**
     * 新增身份认证
     * 
     * @param jiajiaWorkerRenzheng 身份认证
     * @return 结果
     */
    @Override
    public int insertJiajiaWorkerRenzheng(JiajiaWorkerRenzheng jiajiaWorkerRenzheng)
    {           jiajiaWorkerRenzheng.setPayStatus("2");
        return jiajiaWorkerRenzhengMapper.insertJiajiaWorkerRenzheng(jiajiaWorkerRenzheng);
    }

    /**
     * 修改身份认证
     * 
     * @param jiajiaWorkerRenzheng 身份认证
     * @return 结果
     */
    @Override
    public int updateJiajiaWorkerRenzheng(JiajiaWorkerRenzheng jiajiaWorkerRenzheng)
    {
        return jiajiaWorkerRenzhengMapper.updateJiajiaWorkerRenzheng(jiajiaWorkerRenzheng);
    }

    /**
     * 批量删除身份认证
     * 
     * @param ids 需要删除的身份认证主键
     * @return 结果
     */
    @Override
    public int deleteJiajiaWorkerRenzhengByIds(String ids)
    {
        return jiajiaWorkerRenzhengMapper.deleteJiajiaWorkerRenzhengByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除身份认证信息
     * 
     * @param id 身份认证主键
     * @return 结果
     */
    @Override
    public int deleteJiajiaWorkerRenzhengById(Long id)
    {
        return jiajiaWorkerRenzhengMapper.deleteJiajiaWorkerRenzhengById(id);
    }

    @Override
    public void makePayment() {
        JiajiaWorkerRenzheng jiajiaWorkerRenzheng=new JiajiaWorkerRenzheng();
        jiajiaWorkerRenzheng.setPayStatus("2");
        List<JiajiaWorkerRenzheng> jiajiaWorkerRenzhengs = jiajiaWorkerRenzhengMapper.selectJiajiaWorkerRenzhengList(jiajiaWorkerRenzheng);

        jiajiaWorkerRenzhengs.stream().forEach(a ->{
            JiajiaWorker jiajiaWorker = iJiajiaWorkerService.selectJiajiaWorkerById(a.getWid());
            JiajiaUser jiajiaUser = userService.selectjiajiaUser(jiajiaWorker.getInviter());
            JiajiaUser jiajiaWork = userService.selectjiajiaUser(jiajiaWorker.getPhone());


            //收益人
            BigDecimal money = jiajiaUser.getMoney();
            String income = CacheUtils.get(Constants.SYS_CONFIG_CACHE, Constants.SYS_CONFIG_KEY + SysConfigConstants.sys_promotion_worker_income).toString();
            BigDecimal incomeAfter = money.add(new BigDecimal(income));
            jiajiaUser.setMoney(incomeAfter);
            userService.updateJiajiaUser(jiajiaUser);
            //加入流水表
            JiajiaTransferAccounts jiajiaTransferAccounts=new JiajiaTransferAccounts();
            jiajiaTransferAccounts.setUserId(jiajiaUser.getId());
            jiajiaTransferAccounts.setTransferAmount(new BigDecimal(income));
            jiajiaTransferAccounts.setOrderId(jiajiaWorker.getId());
            jiajiaTransferAccounts.setSourceUserId(jiajiaWork.getId());
            jiajiaTransferAccounts.setOrderType("4");
            jiajiaTransferAccounts.setTransferType("9");
            jiajiaTransferAccounts.setRemark("推广社工收益");
            jiajiaTransferAccounts.setVersion(LocalDateTime.now()+String.valueOf(Math.random()));
            jiajiaTransferAccounts.setDeleted("1");

            jiajiaTransferAccountsService.insertJiajiaTransferAccounts(jiajiaTransferAccounts);
            a.setPayStatus("1");
            jiajiaWorkerRenzhengMapper.updateJiajiaWorkerRenzheng(a);

/*
            JiajiaWorker jiajiaWorker = iJiajiaWorkerService.selectJiajiaWorkerById(a.getWid());
            JiajiaUser jiajiaUser = iJiajiaUserService.selectjiajiaUser(jiajiaWorker.getInviter());
            TransferBillsVo transferBillsVo =new TransferBillsVo();
            //转账唯一号
            transferBillsVo.setOutBillNo(WxPayKit.generateStr());
            //转账金额
            Object amount = CacheUtils.get(Constants.SYS_CONFIG_CACHE, Constants.SYS_CONFIG_KEY+ SysConfigConstants.sys_promotion_worker_income);
            Integer transferAmount=Integer.valueOf(amount.toString());
            //转账备注
            transferBillsVo.setTransferType("9");
            transferBillsVo.setTransferRemark("推广社工收益");
            transferBillsVo.setTransferAmount(transferAmount);
            transferBillsVo.setOrderId(a.getId());
            transferBillsVo.setOrderType("4");
            transferBillsVo.setUserId(jiajiaUser.getId());
            transferBillsVo.setSourceUserId(jiajiaWorker.getWid());
            //转账场景报备
            List<TransferSceneReportInfo> list=new ArrayList<>();
            TransferSceneReportInfo transferSceneReportInfo=new TransferSceneReportInfo();
            TransferSceneReportInfo transferSceneReportInfo1=new TransferSceneReportInfo();
            transferSceneReportInfo.setInfoType("活动名称");
            transferSceneReportInfo.setInfoContent("推广有奖");
            transferSceneReportInfo1.setInfoType("奖励说明");
            transferSceneReportInfo1.setInfoContent("推广社工一次性现金奖励");
            list.add(transferSceneReportInfo);
            list.add(transferSceneReportInfo1);
            transferBillsVo.setTransferSceneReportInfos(list);

            //转账用户
            String openId="";
            if(Optional.ofNullable(jiajiaUser.getOpenId()).isPresent() && !jiajiaUser.getOpenId().equals("")){
                openId=jiajiaUser.getOpenId();
                transferBillsVo.setPaymentChannels(TokenUtil.platforms_MP);
            }else if(Optional.ofNullable(jiajiaUser.getGzhOpenId()).isPresent()&& !jiajiaUser.getGzhOpenId().equals("")){
                openId=jiajiaUser.getGzhOpenId();
                transferBillsVo.setPaymentChannels(TokenUtil.platforms_H5);
            }else{
                openId=jiajiaUser.getAppOpenId();
                transferBillsVo.setPaymentChannels(TokenUtil.platforms_APP);
            }
            transferBillsVo.setOpenid(openId);
            apiPayNotifyService.transferBills( transferBillsVo);*/

        });


    }
}
